package com.example.myapplication.ui.activity.square

import android.app.Activity
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.ui.vm.SharedViewModel
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.util.ToastUtils
import kotlinx.android.synthetic.main.activity_add_collection.*
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *分享文章
 *
 * @author : Kun
 * @date : 2021/6/10
 * @time : 14:07
 */
@Route(path = ARouterPath.SHARE_ARTICLE)
class ShareArticleActivity : AbsBaseActivity() {

    private lateinit var shareArticleViewModel: ShareArticleViewModel
    override fun getLayoutId(): Int {
        return R.layout.activity_share_article
    }

    override fun initViewModel() {
        shareArticleViewModel = getActivityScopeViewModel(ShareArticleViewModel::class.java)
        shareArticleViewModel.squareRequest.sharedArticleResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                setResult(Activity.RESULT_OK)
                finish()
            }
        })
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        tv_title.text = "分享文章"
        tv_right.text = "提交"
        tv_right.visibility = View.VISIBLE
        tv_right.setOnClickListener {
            submit()
        }
    }

    /**
     * 提交
     */
    private fun submit() {
        val title = et_title.text.toString()
        val url = et_url.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShortToast("请输入标题")
            return
        }

        if (TextUtils.isEmpty(url)) {
            ToastUtils.showShortToast("请输入链接地址")
            return
        }
        shareArticleViewModel.requestSharedArticle(title, url)

    }
}